package com.wanmait.movies.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.movies.dao.MovieCountryDao;
import com.wanmait.movies.dao.MovieDao;
import com.wanmait.movies.dao.MovieLanguageDao;
import com.wanmait.movies.dao.MovieTypeDao;
import com.wanmait.movies.vo.Movie;
import com.wanmait.movies.vo.MovieCountry;
import com.wanmait.movies.vo.MovieLanguage;
import com.wanmait.movies.vo.MovieType;

/**
 * Servlet implementation class TagServlet
 */

@WebServlet("/TagServlet")
public class TagServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TagServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String action = request.getParameter("action");
		if("tagsearch".equals(action)) {
			tagsearch( request, response);
		}
		if("checkTagOfMovieAjax".equals(action)) {
			checkTagOfMovieAjax( request, response);
		}
	}
	protected void checkTagOfMovieAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String movietype = request.getParameter("movietype");
		String moviecountry = request.getParameter("moviecountry");
		String movielanguage = request.getParameter("movielanguage");
		ArrayList<Movie> movies = MovieDao.getMovieDao().getMoviesWithTags(movietype, moviecountry, movielanguage);
		request.setAttribute("moviesoftag", movies);
		request.getRequestDispatcher("/list2.jsp").forward(request, response);
	}
	protected void tagsearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ArrayList<MovieType> alltype = MovieTypeDao.getDao().getTypeList();
		ArrayList<MovieCountry> allcountry = MovieCountryDao.getDao().getCountryList();
		ArrayList<MovieLanguage> alllang = MovieLanguageDao.getDao().getLanguageList();
		//ArrayList<Movie> alltime = MovieDao.getMovieDao().getMovieShowtimeList();
		ArrayList<Movie> recommend = MovieDao.getMovieDao().getRecommendMovie();
		 
		request.setAttribute("alltype", alltype);
		request.setAttribute("allcountry", allcountry);
		request.setAttribute("alllang", alllang);
		//request.setAttribute("alltime", alltime);
		request.setAttribute("recommend", recommend);
		
		request.getRequestDispatcher("/tag.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
